package xuan.mall.system.domain.service.impl;

import lombok.RequiredArgsConstructor;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;
import xuan.mall.system.domain.entity.MallUserDetails;
import xuan.mall.system.domain.service.AuthDomainService;
import xuan.mall.system.infrastructure.security.util.MallJWTUtil;
import xuan.mall.system.interfaces.dto.response.Response;
import xuan.mall.system.interfaces.dto.response.TokenDTO;

/**
 * @author: rongxuanzhou
 * @create: 2025/6/1
 * @Description:
 */
@Service
@RequiredArgsConstructor
public class AuthDomainServiceImpl implements AuthDomainService {

    private final AuthenticationManager authenticationManager;

    @Override
    public Response<TokenDTO> login(String username, String password) {
        UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, password);
        Authentication authentication = authenticationManager.authenticate(token);
        MallUserDetails details = (MallUserDetails) authentication.getPrincipal();
        return Response.success(new TokenDTO(MallJWTUtil.createToken(details)));
    }
}
